package org.hang.jvm.classpath.impl;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * 通配符类路径，继承 CompositeEntry
 *
 * @author 廖景航
 * @date created on 2022/2/23 下午4:00
 */
public class WildcardEntry extends CompositeEntry {
    public WildcardEntry(String path) {
        super(toPathList(path));
    }

    private static String toPathList(String wildcardPath) {
        String baseDir = wildcardPath.replace("*", "");
        try (Stream<Path> pathStream = Files.walk(Paths.get(baseDir))) {
            return pathStream.filter(Files::isRegularFile).map(Path::toString).filter(p -> p.endsWith(".jar") || p.endsWith("JAR")).collect(Collectors.joining(File.pathSeparator));
        } catch (IOException e) {
            return "";
        }
    }


}
